import Vue from 'vue'
import Router from 'vue-router'
import NProgress from 'nprogress'//引入nprogress
import 'nprogress/nprogress.css' //这个样式必须引入

NProgress.inc(0.2)
NProgress.configure({showSpinner: false})

// 解决ElementUI导航栏中的vue-router在3.0版本以上重复点菜单报错问题
const originalPush = Router.prototype.push
Router.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}

Vue.use(Router)


const router = new Router({
  routes: [
    {
      path: '*',
      redirect: '/home'
    },
    {
      path: '/home',
      name: '首页',
      component: () => import ('@/pages/home'),
      meta: {keepAlive: true, order: 0, title: "首页", requireAuth: true}
    },
    {
      path: '/login',
      name: '登录',
      component: () => import ('@/pages/login'),
      meta: {keepAlive: false}
    },
    {
      path: '/withdraw',
      name: '提现',
      component: () => import ('@/pages/withdraw'),
      meta: {keepAlive: true, order: 1, title: "提现", requireAuth: true}
    },
    {
      path: '/profile',
      name: "我的",
      component: () => import ('@/pages/profile'),
      meta: {keepAlive: true, order: 2, requireAuth: true}
    },
    {
      path: '/bindWechat',
      name: "绑定微信",
      component: () => import ('@/pages/bind-wechat'),
      meta: {keepAlive: false, title: "绑定微信", back: "on", requireAuth: true}
    },
    {
      path: '/changePwd',
      name: "修改密码",
      component: () => import ('@/pages/change-password'),
      meta: {keepAlive: false, title: "修改密码", back: "on", requireAuth: true}
    },
    {
      path: '/orderList',
      name: "提现订单",
      component: () => import ('@/pages/withdraw/order-list'),
      meta: {keepAlive: false, title: "提现订单", back: "on", requireAuth: true}
    },
    {
      path: '/transactionList',
      name: "交易统计列表",
      component: () => import ('@/pages/transaction-list'),
      meta: {keepAlive: false, title: "交易统计列表", back: "on", requireAuth: true}
    },
    {
      path: '/withdrawDevice',
      name: "设备SN列表",
      component: () => import ('@/pages/withdraw/withdraw-device'),
      meta: {keepAlive: false, title: "设备SN列表", back: "on", requireAuth: true}
    }
  ]
})

router.beforeEach((to, from, next) => {
  NProgress.start()
  if (to.path === '/login') {
    next();
  } else {
    let token = localStorage.getItem('token');
    if (!token || token === '') {
      next('/login');
    } else {
      next();
    }
  }
});

router.afterEach(() => {
  NProgress.done()
})

export default router
